// packageI/knowledge/knowledgeSearch/knowledgeSearch.js
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    keyword: "",
    list: [],
    logo_url: "",
    last_page: "",
    finished: false,
    loading: false,
    current_page: 1,
    keyWorkds: [],

    plugin: 'message-base',//message-base商城知识库，project-template供应链知识库
    apiOption: {},
    pluginOption: {},
    itemOption: {
      article_name: 'article_name',
      creater_name: 'creater_name',
      base_name: 'base_name',
      created_at: 'created_at'
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.keyword = options.keyword || "";
    this.init();
    this.getData();
  },

  init(){
    this.data.list = [];
    this.data.current_page = 1;
    this.data.finished = false;
    this.data.loading = false;
    this.data.pluginOption = {
      'message-base': {
        index: 'plugin.message-base.frontend.article.index',
        params: ()=> {
         return {keyword: this.data.keyword, page: this.data.current_page}
        },
        callback:(res)=>{
          let { data, logo, current_page, last_page } = res.data;
          let list = this.data.keyword == "" ? data : data.filter(r => {
            let str = this.data.keyword;
            let newstr = `<span style="color:#e44848;">${this.data.keyword}</span>`;
            r.article_name = r.article_name.replace(str, newstr);
            return r;
          });
          this.data.list = this.data.list.concat(list);
          if (current_page >= last_page) this.data.finished = true;
          this.setData({
            current_page: this.data.current_page + 1,
            list: this.data.list,
            logo_url: logo,
            keyWorkds: res.data.keyword || this.data.keyWorkds
          })
        }},
        "project-template": {
          index: 'plugin.project-template.frontend.knowledge.searchKnowledgeBaseList',
          params: ()=> {
            return {title: this.data.keyword, page: this.data.current_page}
          },
          callback:(res)=>{
            this.current_page += 1;
            let { data, current_page, last_page } = res.data;
            let list = this.data.keyword == "" ? data : data.filter(r => {
              let str = this.data.keyword;
              let newstr = `<span style="color:#e44848;">${this.data.keyword}</span>`;
              r.article_name = r.article_name&&r.article_name.replace(str, newstr)||'';
              return r;
            });
            this.data.list = this.data.list.concat(list);
            if (current_page >= last_page) this.data.finished = true;
  
            this.setData({
              list: this.data.list,
              keyWorkds: res.data.keyword || this.data.keyWorkds,
              itemOption: {
                article_name: 'title',
                base_name: 'has_one_yz_supply_knowledge_base.title',
                created_at: 'release_time_at'
              }
            })
          }
        },
        "project-template-good": {
          flag: 'project-template',
          index: 'plugin.project-template.frontend.knowledge.searchKnowledgeBaseArticleList',
          params: ()=> {
            return {title: this.data.keyword, page: this.data.current_page,goods_id: this.options.gid}
          },
          callback:(res)=>{
            this.data.current_page += 1;
            let { data, current_page, last_page } = res.data;
            let list = this.data.keyword == "" ? data : data.filter(r => {
              let str = this.keyword;
              let newstr = `<span style="color:#e44848;">${this.data.keyword}</span>`;
              r.article_name = r.article_name&&r.article_name.replace(str, newstr)||'';
              return r;
            });
            this.data.list = this.data.list.concat(list);
            if (current_page >= last_page) this.data.finished = true;
            this.setData({
              list: this.data.list,
              keyWorkds: res.data.keyword || this.data.keyWorkds,
              itemOption: {
                article_name: 'title',
                base_name: 'has_one_yz_supply_knowledge_base.title',
                created_at: 'release_time_at'
              }
            })
          }}
      }

    let {keyword,current_page} = this.data;
    // let api = new Map([
    //   ["message-base", {
    //     index: 'plugin.message-base.frontend.article.index',
    //     params: ()=> {
    //       return {keyword: keyword, page: current_page}
    //     },
    //     callback:(res)=>{
    //       let { data, logo, current_page, last_page } = res.data;
    //       let list = this.data.keyword == "" ? data : data.filter(r => {
    //         let str = eval(`/${this.data.keyword}/g`);
    //         let newstr = `<span style="color:#e44848;">${this.data.keyword}</span>`;
    //         r.article_name = r.article_name.replace(str, newstr);
    //         return r;
    //       });
    //       this.data.list = this.data.list.concat(list);
    //       if (current_page >= last_page) this.data.finished = true;
    //       this.setData({
    //         current_page: this.data.current_page + 1,
    //         list: this.data.list,
    //         logo_url: logo,
    //         keyWorkds: res.data.keyword || this.data.keyWorkds
    //       })
    //     }}
    //   ],
    //   ["project-template", {
    //     index: 'plugin.project-template.frontend.knowledge.searchKnowledgeBaseList',
    //     params: ()=> {
    //       return {title: this.data.keyword, page: this.data.current_page}
    //     },
    //     callback:(res)=>{
    //       this.current_page += 1;
    //       let { data, current_page, last_page } = res.data;
    //       let list = this.data.keyword == "" ? data : data.filter(r => {
    //         let str = eval(`/${this.data.keyword}/g`);
    //         let newstr = `<span style="color:#e44848;">${this.data.keyword}</span>`;
    //         r.article_name = r.article_name&&r.article_name.replace(str, newstr)||'';
    //         return r;
    //       });
    //       this.data.list = this.data.list.concat(list);
    //       if (current_page >= last_page) this.data.finished = true;

    //       this.setData({
    //         list: this.data.list,
    //         keyWorkds: res.data.keyword || this.data.keyWorkds
    //       })
    //       this.itemOption = {
    //         article_name: 'title',
    //         base_name: 'has_one_yz_supply_knowledge_base.title',
    //         created_at: 'release_time_at'
    //       };
    //     }}
    //   ],
    //   ["project-template-good", {
    //     flag: 'project-template',
    //     index: 'plugin.project-template.frontend.knowledge.searchKnowledgeBaseArticleList',
    //     params: ()=> {
    //       return {title: this.data.keyword, page: this.data.current_page,goods_id: this.options.gid}
    //     },
    //     callback:(res)=>{
    //       this.data.current_page += 1;
    //       let { data, current_page, last_page } = res.data;
    //       let list = this.data.keyword == "" ? data : data.filter(r => {
    //         let str = eval(`/${this.keyword}/g`);
    //         let newstr = `<span style="color:#e44848;">${this.data.keyword}</span>`;
    //         r.article_name = r.article_name&&r.article_name.replace(str, newstr)||'';
    //         return r;
    //       });
    //       this.data.list = this.data.list.concat(list);
    //       if (current_page >= last_page) this.data.finished = true;
    //       this.setData({
    //         list: this.data.list,
    //         keyWorkds: res.data.keyword || this.data.keyWorkds
    //       })
    //       this.itemOption = {
    //         article_name: 'title',
    //         base_name: 'has_one_yz_supply_knowledge_base.title',
    //         created_at: 'release_time_at'
    //       };
    //     }}
    //   ],
    // ]);
    if(this.options.plu) {
      this.data.plugin = this.options.plu;
    }
    this.data.apiOption = this.data.pluginOption[this.data.plugin],
    this.setData({
      getPlugin: this.data.apiOption.flag?this.data.apiOption.flag:this.data.plugin
    });
  },
  onChange(e) {
    this.setData({
      keyword: e.detail,
    });
  },
  search() {
    this.init();
    this.getData();
  },
  clickKeyword(e) {
    let text = e.target.dataset.key||e.currentTarget.dataset.key;
    this.setData({
      keyword: text
    })
    this.search();
  },
  getData() {
    if (this.data.finished || this.data.loading) return;
    this.data.loading = true;
    let urlStr = app.getNetAddresss(this.data.apiOption.index);
    app._getNetWork({
      url: urlStr,
      data: this.data.apiOption.params(),
      success: (res) => {
        this.data.loading = false;
        if (res.data.result !== 1) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          });
          return;
        }
        this.data.apiOption.callback(res.data);
      }
    });
  },
  wxRouterLink(_data) {
    let _Param = '';
    const {
      url,
      ...otherParam
    } = _data;
    Object.keys(otherParam).forEach(function (key) {
      if (otherParam[key] !== '') _Param += key + '=' + otherParam[key] + '&'
    });
    if (url === '') return;
    wx.navigateTo({
      url: url + '?' + _Param.slice(0, -1)
    })
  },
  onListClick(e){
    let {id,base_id,knowledge_base_id} = e.detail;
    this.wxRouterLink({url: '/packageI/knowledge/knowledgeDetail/knowledgeDetail',id: id,bid: base_id||knowledge_base_id,plu: this.data.getPlugin});
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getData();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})